import 'package:dio/dio.dart';
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';

void main() async {
  print("start");
  await Spider.search("西游记");
  print("stop");
}

class Spider {
  static search(String searchContent) async {
    String url = "https://www.biquge.com.cn/search.php?q=" + searchContent;
    // 获取书籍的详情信息
    // todo 先查询数据库中是否存了
    Response resp = await Dio().get(url);
    String data = resp.data;
    // 解析页面元素
    Document documnet = parse(data);
    // 解析book相关信息
    List<Element> resultList = documnet.getElementsByClassName("result-item result-game-item");
    List result = [];
    for(var item in resultList){
      String bookCoverUrl = item.getElementsByTagName("img")[0].attributes["src"];
      String bookTitle = item.getElementsByTagName("span")[0].innerHtml;
      String bookUrl = item.querySelector(".result-game-item-title-link").attributes["href"];
      List<Element> spans = item.querySelector(".result-game-item-info").getElementsByTagName("span");
      String author = spans[1].innerHtml;
      String latestChapter = item.querySelector(".result-game-item-info").getElementsByTagName("a")[0].innerHtml;
      result.add(
        {
          "bookCoverUrl": bookCoverUrl,
          "bookTitle": bookTitle,
          "bookUrl": "https://www.biquge.com.cn" + bookUrl,
          "author": author,
          "latestChapter": latestChapter
        }
      );
    }
    return result;
  }
}
